import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

///
/// 自带搜索框的Appbar
///
class QSearchAppBar extends AppBar {
  QSearchAppBar({
    Key? key,
    required BuildContext buildContext, // 面板
    required TextEditingController searchController, // 控制器
    TextInputType type = TextInputType.none, // 输入类型
    String holdText = "搜索内容", // 默认显示文本
    String? backImgPath, // 返回图片地址
    String? searchIconName, // 搜索图片地址
    Function? searchAction, // 搜索回调
    List<TextInputFormatter>? inputFormatters, // 输入格式
  }) : super(
    key: key,
    elevation: 0,
    centerTitle: true,
    backgroundColor: const Color(0xFFFFFFFF),
    automaticallyImplyLeading: false,
    title: Row(
      children: [
        GestureDetector(
          onTap: () {
            Navigator.pop(buildContext);
          },
          child: backImgPath == null
              ? const Icon(Icons.arrow_back_ios, color: Colors.black, size: 22,)
              : Image.asset(
            backImgPath,
            width: 22,
            height: 22,
          ),
        ),
        Expanded(
            child: Container(
              margin: const EdgeInsets.symmetric(horizontal: 11),
              padding: const EdgeInsets.symmetric(
                horizontal: 11,
              ),
              height: 32,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(16),
                  color: const Color(0xFFF2F2F2)),
              child: Row(
                children: [
                  searchIconName == null
                      ? const Icon(Icons.search, color: Colors.black)
                      : Image.asset(
                    'img/my_page_icon/get_in_car_search.png',
                    width: 15,
                    height: 15,
                  ),
                  Expanded(
                      child: Container(
                          margin: const EdgeInsets.only(left: 7),
                          child: TextField(
                            controller: searchController,
                            inputFormatters: inputFormatters,
                            keyboardType: type,
                            style: const TextStyle(
                                fontSize: 15, color: Colors.black),
                            decoration: InputDecoration(
                                border: InputBorder.none,
                                hintText: holdText,
                                hintStyle: const TextStyle(
                                  fontSize: 15,
                                  color: Color(0xFF999999),
                                ),
                                isDense: true,
                                contentPadding:
                                const EdgeInsets.only(top: 1)),
                            onSubmitted: (value) {
                              searchAction!();
                            },
                          )))
                ],
              ),
            )),
        backImgPath == null
            ? Container()
            : Image.asset(
          backImgPath,
          width: 22,
          height: 22,
          color: Colors.transparent,
        ),
      ],
    ),
  );
}
